import {EventEnum, ItemStatusEnum, ItemTypeEnum, SceneEnum, TriggerStatusEnum} from "db://assets/Scripts/Enum";
import Singleton from "db://assets/Scripts/Base/Singleton";
import EventManager from "db://assets/Scripts/Runtime/EventManager";
import {sys} from "cc";

interface IItem {
    status: ItemStatusEnum
    type: ItemTypeEnum
}

const STORAGE_KEY = 'STORAGE_KEY'

export default class DataManager extends Singleton {
    static get Instance() {
        return super.GetInstance<DataManager>()
    }

    readonly H2AAnswer = [0, 1, 2, 3, 4, 5, null]
    readonly H2AInitData = [1, 0, 3, 2, 5, 4, null]
    private _H2AData = [...this.H2AInitData]

    private _curItemType: ItemTypeEnum | null = null // 记录背包当前的物品
    private _items: Array<IItem> = [
        {type: ItemTypeEnum.Key, status: ItemStatusEnum.Scene,},
        {type: ItemTypeEnum.Mail, status: ItemStatusEnum.Disabled,}, // 用钥匙打开邮箱之后才显示 mail
    ]
    private _isSelected = false
    private _mailboxStatus: TriggerStatusEnum = TriggerStatusEnum.Pending
    private _grandmaStatus: TriggerStatusEnum = TriggerStatusEnum.Pending
    private _grandmaDialogIndex = -1 // 对话框显示的句子索引
    private _doorStatus: TriggerStatusEnum = TriggerStatusEnum.Pending
    private _curScene: SceneEnum = SceneEnum.H1

    get curScene(): SceneEnum {
        return this._curScene;
    }

    set curScene(value: SceneEnum) {
        this._curScene = value;
        this.render()
    }

    get doorStatus(): TriggerStatusEnum {
        return this._doorStatus
    }

    set doorStatus(value: TriggerStatusEnum) {
        this._doorStatus = value
        this.render()
    }

    get H2AData(): (number | null)[] {
        return this._H2AData
    }

    set H2AData(value: (number | null)[]) {
        this._H2AData = value
        this.render()
    }

    get grandmaDialogIndex(): number {
        return this._grandmaDialogIndex
    }

    set grandmaDialogIndex(value: number) {
        this._grandmaDialogIndex = value
        this.render()
    }

    get grandmaStatus() {
        return this._grandmaStatus
    }

    set grandmaStatus(newData) {
        this._grandmaStatus = newData
        this.render()
    }

    get mailboxStatus() {
        return this._mailboxStatus
    }

    set mailboxStatus(newData) {
        this._mailboxStatus = newData
        this.render()
    }

    get isSelected() {  // 记录背包是否被点击选中
        return this._isSelected
    }

    set isSelected(newData) {
        this._isSelected = newData
        this.render()
    }

    get curItemType() {
        return this._curItemType
    }

    set curItemType(newData: ItemTypeEnum) {
        this._curItemType = newData
        this.render()
    }

    get items() {
        return this._items
    }

    set items(newData) {
        this._items = newData
        // 触发渲染
        this.render()
        // EventManager.Instance.emit(EventEnum.Render)
    }

    render() {
        EventManager.Instance.emit(EventEnum.Render)
        this.save() // 每次渲染的时候直接保存数据
    }

    save() {
        // 直接数据保存到本地
        sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            H2AData: this.H2AData,
            curItemType: this.curItemType,
            items: this.items,
            isSelected: this.isSelected,
            mailboxStatus: this.mailboxStatus,
            grandmaStatus: this.grandmaStatus,
            grandmaDialogIndex: this.grandmaDialogIndex,
            doorStatus: this.doorStatus,
            curScene: this.curScene,
        }))
    }

    restore() {
        const _data = sys.localStorage.getItem(STORAGE_KEY)
        try {
            const data = JSON.parse(_data)
            this.H2AData = data.H2AData
            this.curItemType = data.curItemType
            this.items = data.items
            this.isSelected = data.isSelected
            this.mailboxStatus = data.mailboxStatus
            this.grandmaStatus = data.grandmaStatus
            this.grandmaDialogIndex = data.grandmaDialogIndex
            this.doorStatus = data.doorStatus
            this.curScene = data.curScene
        } catch (e) {
            this.reset()
        }
    }

    reset() {
        this.H2AData = [...this.H2AInitData]
        this.curItemType = null
        this.items = [
            {type: ItemTypeEnum.Key, status: ItemStatusEnum.Scene,},
            {type: ItemTypeEnum.Mail, status: ItemStatusEnum.Disabled,},
        ]
        this.isSelected = false
        this.mailboxStatus = TriggerStatusEnum.Pending
        this.grandmaStatus = TriggerStatusEnum.Pending
        this.grandmaDialogIndex = -1
        this.doorStatus = TriggerStatusEnum.Pending
        this.curScene = SceneEnum.H1
    }
}
